/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.fee.core.service.processor.rule;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.business.account.core.domain.account.Account;
import info.peigen.hotpot.business.fee.core.domain.FeeRule;
import info.peigen.hotpot.business.fee.core.service.processor.base.AbstractFeeServiceProcessor;
import info.peigen.hotpot.business.fee.facade.command.order.rule.FeeRuleCreateOrder;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.common.service.base.test.ServiceTestBaseMockConstant;
import org.noear.solon.annotation.Component;


/**
 * <b>(FeeRuleCreateProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/6/1
 */
@Component
public class FeeRuleCreateProcessor extends AbstractFeeServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 20;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        FeeRuleCreateOrder order = (FeeRuleCreateOrder) serviceContext.getOrder();
        FeeRule rule = FeeRule.builder()
                .name(order.getName())
                .stationParentId(order.getStationParentId())
                .partnerId(order.getPartnerId())
                .code(order.getCode())
                .currency(order.getCurrency())
                .method(order.getMethod())
                .type(order.getType())
                .billing(order.getBilling())
                .charge(order.getCharge())
                .context(order.getContext())
                .partnerId(order.getPartnerId())
                .memo(order.getMemo())
                .build();

        if (StrUtil.isNotEmpty(order.getCustomerId())) {
            Account account = accountRepository.active(order.getCustomerId());
            Assert.notNull(account, () -> new BusinessException(ResultCode.FAILURE, "收费规则[客户]不得为空![accountId=]" + order.getCustomerId()));
            rule.setAccount(account);
        }

        if (StrUtil.isNotEmpty(order.getBillingAccountId())) {
            Account billingAccount = accountRepository.active(order.getBillingAccountId());
            Assert.notNull(billingAccount, () -> new BusinessException(ResultCode.FAILURE, "收费规则[计费]客户不得为空![billingAccountId=]" + order.getBillingAccountId()));
            rule.setBillingAccount(billingAccount);
        }

        if (StrUtil.isNotEmpty(order.getPayerAccountId())) {
            Account payerAccount = accountRepository.active(order.getPayerAccountId());
            Assert.notNull(payerAccount, () -> new BusinessException(ResultCode.FAILURE, "收费规则[付费]客户不得为空![payerAccountId=]" + order.getPayerAccountId()));
            rule.setPayerAccount(payerAccount);
        }

        if (StrUtil.isNotEmpty(order.getPayeeAccountId())) {
            Account payeeAccount = accountRepository.active(order.getPayeeAccountId());
            Assert.notNull(payeeAccount, () -> new BusinessException(ResultCode.FAILURE, "收费规则[收费]客户不得为空![payeeAccountId=]" + order.getPayeeAccountId()));
            rule.setPayeeAccount(payeeAccount);
        } else {
            if (StrUtil.isNotEmpty(rule.getPartnerId())) {
                Account partnerPayeeAccount = accountRepository.active(rule.getPartnerId());
                rule.setPayeeAccount(partnerPayeeAccount);
            } else {
                Account defaultPayeeAccount = accountRepository.active(ServiceTestBaseMockConstant.SELF_PARTNER_ID);
                rule.setPayerAccount(defaultPayeeAccount);
            }
        }

        serviceContext.setDomain(rule);
    }
}
